package com.wanghua.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author wanghua
 * @create 2021-08-21 11:26
 * 自定义的检验器
 */
public class ListIntValueConstraintValidator implements ConstraintValidator<ListIntValue, Integer> {

    private final Set<Integer> set = new HashSet<>();

    //初始化
    @Override
    public void initialize(ListIntValue constraintAnnotation) {
        set.clear();
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            set.add(value);
        }
    }

    /**
     * @param value   提交的值
     * @param context 上下文信息
     * @return 是否校验成功
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
